﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace mtdsharp
{
    class Route
    {
        private string id;
        public string ID
        {
            get { return id; }
        }

        private int number;
        public int Number
        {
            get { return number; }
        }

        private string name;
        public string Name
        {
            get { return name; }
        }

        private Color color;
        public Color Color
        {
            get { return color; }
        }

        private Color textcolor;
        public Color TextColor
        {
            get { return textcolor; }
        }

        private Image emblem;
        public Image Emblem
        {
            get { return emblem; }
        }

        private List<Trip> trips;
        public List<Trip> Trips
        {
            get { return trips; }
        }

        public Route(string gtfsLine, int emblemSize)
        {
            // Split up the comma-separated values from the GTFS data line
            string[] gtfsElements = gtfsLine.Split(',');

            // Set the ID, Number and Name properties
            this.id = gtfsElements[0];
            this.number = Convert.ToInt32(gtfsElements[2]);
            this.name = gtfsElements[3];

            // Parse the string representing the route color and store it as a Color object
            int rgb = Convert.ToInt32(gtfsElements[7], 16);
            int r = rgb >> 16 & 0xff;
            int g = rgb >> 8 & 0xff;
            int b = rgb & 0xff;
            this.color = Color.FromArgb(r, g, b);

            // Same as route color but for its text color
            int rgbt = Convert.ToInt32(gtfsElements[8], 16);
            int rt = rgbt >> 16 & 0xff;
            int gt = rgbt >> 8 & 0xff;
            int bt = rgbt & 0xff;
            this.textcolor = Color.FromArgb(rt, bt, gt);

            // Begin creating the route emblem by creating a blank Bitmap
            this.emblem = new Bitmap(emblemSize, emblemSize);

            // Declare our drawing canvas and brush
            Graphics canvas = Graphics.FromImage(this.emblem);
            Brush textBrush = new SolidBrush(this.textcolor);

            // Declare a StringFormat for aligning the number on the center of the emblem
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            // Fill the square bitmap with the route color
            canvas.Clear(this.color);

            // Draw the number on this square
            canvas.DrawString(this.number.ToString(), new Font("Segoe UI", (int)(emblemSize * 0.35)), textBrush, emblemSize / 2, emblemSize / 2, format);

            // Commit these changes to the Emblem image
            canvas.Save();
        }
    }
}
